package snapex.core.mapper;

import javax.naming.NamingException;
import javax.naming.directory.Attributes;

import org.springframework.ldap.core.AttributesMapper;
import org.springframework.stereotype.Component;

import snapex.core.model.Employee;

@Component
public class EmployeeMapper implements AttributesMapper<Employee> {
	@Override
	public Employee mapFromAttributes(Attributes result) throws NamingException {
		Employee staff = new Employee();
		staff.setStaffId(getAttribute(result, "cn"));
		staff.setDistinguishedName(getAttribute(result, "distinguishedName"));
		staff.setFirstName(getAttribute(result, "hsbc-ad-KnownAsGivenName"));
		staff.setLastName(getAttribute(result, "hsbc-ad-KnownAsLastName"));
		staff.setTitle(getAttribute(result, "personalTitle"));
		staff.setCountryCode(getAttribute(result, "c"));
		staff.setCompanyCode(getAttribute(result, "hsbc-ad-CompanyCode"));
		staff.setLocationId(getAttribute(result, "l"));
		staff.setDepartmentId(getAttribute(result, "hsbc-ad-DepartmentID"));
		staff.setRole(getAttribute(result, "hsbc-ad-WorkRole"));
		staff.setMail(getAttribute(result, "mail"));
		
		staff.setDepartment(getAttribute(result, "departpment"));
		staff.setExtensionNumber(getAttribute(result, "hsbc-ad-ExtTelLocalNo"));
		staff.setManagerId(getAttribute(result, "hsbc-ad-LineManagerID"));
		staff.setManagerName(getAttribute(result, "hsbc-ad-LineManagerName"));
		
		return staff;
	}

	protected String getAttribute(Attributes result, String attribute) throws NamingException {
		String value = "";
		if (result.get(attribute) != null) {
			value = result.get(attribute).get().toString();
		}
		return value;
	}
}